//logs.js
const util = require('../../utils/util.js')
const net = require('../../utils/request.js');
const app = getApp();

var key = require('../../utils/MD5.js');

Page({
  data: {
    phoneNum:'',
    passworld:'',
    nickName: '',//昵称
    avatarUrl: '',//头像
    form_id:'',
    channel:'0',
  },
  onLoad: function (res) {

    var that = this;
    that.getopenid();//拿去openid
    if(res.channel){
      wx.setStorage({
        key: "channel",
        data: res.channel
      });
    }
    that.setData({
      channel: res.channel,// 小程序渠道 0 为签到，1是抽奖，2 砍价
    });
  },


  onReady:function(){
    
    var that = this;
    wx.getUserInfo({
      success: function (ress) {
        console.log(ress);
        that.setData({
          nickName: ress.userInfo.nickName,
          avatarUrl: ress.userInfo.avatarUrl,
        });

      }
    });
  },
  sendFormId:function(e){

    var that = this;
    that.setData({
      form_id: e.detail.formId,  
    });
    
  },

  loginBtn:function(e) {
   
  var that = this;
      //如果没有拿到过openid
      if (app.globalData.openid && app.globalData.openid.length !=0) {
        that.loginAction(e);
      } else {
        wx.login({
          success: function (res) {
            var code = res.code;
            //授权操作，获取openid
            net.requestHttp('welfare/index/getopenid',
              { code: code },
              function (res) {
                if(res.data.state==1){
                  wx.setStorage({
                    key: "openid",
                    data: res.data.data.openid
                  }),
                    wx.getUserInfo({
                      success: function (ress) {
                        console.log(ress);
                        that.setData({
                          nickName: ress.userInfo.nickName,
                          avatarUrl: ress.userInfo.avatarUrl,
                        });
                      }
                    });
                  app.globalData.openid = res.data.data.openid;
                  app.globalData.session_key = res.data.data.session_key;

                  that.loginAction(e);
                }else{
                  //登录失败
                }
              }
            )
          }
        });
      
      }
    
  },
  //输入密码
  inputPassworld:function(e){
    var that = this;
    that.setData({
      passworld: e.detail.value
    })
  },
  //输入电话号码
  inputPhoneNum: function (e) {
    var that = this;
    that.setData({
      phoneNum: e.detail.value
    })
  },
  registAction:function(){
    wx.redirectTo({
      url: '../regist/regist',
    })
  },
  loginAction:function(res){

    var that = this;
    if (that.data.phoneNum.length < 11) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none'
      })
      return;
    }
    if(that.data.phoneNum.length==0){
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
      return;
    }


    wx.getUserInfo({
      success: function (ress) {
        
        that.setData({
          nickName: ress.userInfo.nickName,
          avatarUrl: ress.userInfo.avatarUrl,
        });

        //授权操作，获取openid
        net.requestHttp('welfare/index/login',
          {
            mobile: that.data.phoneNum,
            password: key.md5(that.data.passworld),
            openid: app.globalData.openid,
            form_id: that.data.form_id,
            headimgurl: ress.userInfo.avatarUrl,
            nickname: ress.userInfo.nickName,
            channel:that.data.channel,
          },
          function (res) {
            if (res.data.state == 1) {
              wx.setStorage({
                key: 'userInfo',
                data: res.data.data,
              })
              app.globalData.userInfo = res.data.data;
              wx.navigateBack({});
            }
          }
        )
      }
    });
    
  },
  getopenid: function () {
    var that = this;
    wx.login({
      success: function (res) {
        var code = res.code;
        //授权操作，获取openid
        net.requestHttp('welfare/index/getopenid',
          { code: code },
          function (res) {
            if (res.data.state == 1) {
              wx.setStorage({
                key: "openid",
                data: res.data.data.openid
              }),
                wx.getUserInfo({
                  success: function (ress) {
                    console.log(ress);
                    that.setData({
                      nickName: ress.userInfo.nickName,
                      avatarUrl: ress.userInfo.avatarUrl,
                    });
                  }
                });
              app.globalData.openid = res.data.data.openid;
              app.globalData.session_key = res.data.data.session_key;
            } else {
              //登录失败
            }
          }
        )
      }
    });
  },
})
